package cn.javaee.springmvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.javaee.springmvc.model.Address;
import cn.javaee.springmvc.model.User;
import cn.javaee.springmvc.service.UserService;

@Controller
public class RegistrationController {
  @Autowired
  public UserService userService;
  @RequestMapping(value = "/register", method = RequestMethod.GET)
  public ModelAndView showRegister(HttpServletRequest request, HttpServletResponse response) {
    ModelAndView mav = new ModelAndView("register");
    mav.addObject("user", new User());
    mav.addObject("address",new Address());
    return mav;
  }
  @RequestMapping(value = "/registerProcess", method = RequestMethod.POST)
  public String addUser(User user,Model m) {
	  System.out.println(user);
	  userService.registerUser(user);
	  userService.registerAddress(user, user.getAddress().get(0));
	  userService.registerAddress(user, user.getAddress().get(1));
	  m.addAttribute("name", user.getName());
	  return "welcome";
  }
}

